package com.yoomiito.app.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;

import androidx.annotation.Nullable;

import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.utils.DensityUtil;
import com.yoomiito.app.widget.dialog.AlipayH5Dialog;
import com.yoomiito.app.widget.dialog.InputNumDialog;

/**
 * 数字加减控制器
 */

public class NumControlView extends LinearLayout implements View.OnClickListener {

    private static final int NUM_TEXT_SIZE = 16;
    private static final int NUM_TEXT_COLOR = Color.parseColor("#999999");
    private static final int SYMBOL_SIZE = 18;
    private static final int SYMBOL_COLOR = Color.parseColor("#DEDEDE");
    private static final int NUM = 1;
    private static final int MINUS_TEXT_ID = 0;
    private static final int ADD_TEXT_ID = 1;
    private static final int NUM_TEXT_ID = 2;
    private static final int MAXVALUE = 50;
    private Context mContext;
    private int numTextSize;
    private int numTextColor;
    private int symbolSize;
    private int symbolColor;
    private int num = NUM;
    private int step = 1;
    private int max = 10;
    private int min = 0;
    private TextView numTv;
    private int maxVlaue = MAXVALUE;
    private OnNumberChangeListener listener;
    private boolean mNoClick = true;
    private String content = "购买数量不能小于0";
    private String content1 = "购买数量不能大于";
    private boolean isCar = false;
    ImageView minIv;
    private OnInputNumChangeListener mOnInputNumChangeListener;

    public NumControlView(Context context) {
        this(context, null);
    }

    public NumControlView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NumControlView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(attrs);
        initView();
    }

    private void initAttrs(AttributeSet attrs) {
        mContext = getContext();
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.NumControlView);
        numTextSize = ta.getDimensionPixelSize(R.styleable.NumControlView_num_text_size, DensityUtil.dp2px(NUM_TEXT_SIZE));
        num = ta.getInteger(R.styleable.NumControlView_num, num);
        numTextColor = ta.getColor(R.styleable.NumControlView_num_text_color, NUM_TEXT_COLOR);
        symbolSize = ta.getDimensionPixelSize(R.styleable.NumControlView_symbol_size, DensityUtil.dp2px(SYMBOL_SIZE));
        symbolColor = ta.getColor(R.styleable.NumControlView_symbol_color, SYMBOL_COLOR);
        ta.recycle();
    }

    private void initView() {
        //减
//        TextView minusTv = getTextView();
//        setSymbolTextView(minusTv,MINUS_TEXT_ID,"—",R.drawable.rect_divider_left_0);
//        LayoutParams lp_1 = new LayoutParams(0, LayoutParams.MATCH_PARENT,0.9f);
//        addView(minusTv,lp_1);

        minIv = new ImageView(mContext);
        minIv.setOnClickListener(this);
        minIv.setId(MINUS_TEXT_ID);
        minIv.setImageResource(R.drawable.addition);
        LayoutParams lp_1 = new LayoutParams(0, LayoutParams.MATCH_PARENT, 0.9f);
        int margin = DensityUtil.dp2px(8);
        minIv.setPadding(margin, margin, margin, margin);
        addView(minIv, lp_1);

        //数字
        numTv = getTextView();
        numTv.setId(NUM_TEXT_ID);
        numTv.setOnClickListener(this);
        numTv.setBackgroundResource(R.drawable.rect_divider_0);
        setNumTextView(numTv);
        LayoutParams lp_2 = new LayoutParams(0, LayoutParams.MATCH_PARENT, 1.2f);
        addView(numTv, lp_2);
        //加
//        TextView addTv = getTextView();
//        setSymbolTextView(addTv,ADD_TEXT_ID,"+",R.drawable.rect_divider_right_0);
//        LayoutParams lp_3 = new LayoutParams(0, LayoutParams.MATCH_PARENT,0.9f);
//        addView(addTv,lp_3);
        ImageView addIv = new ImageView(mContext);
        addIv.setPadding(margin, margin, margin, margin);
        addIv.setId(ADD_TEXT_ID);
        addIv.setOnClickListener(this);
        addIv.setImageResource(R.drawable.subtraction);
        LayoutParams lp_3 = new LayoutParams(0, LayoutParams.MATCH_PARENT, 0.9f);
        addView(addIv, lp_3);

        setBackgroundResource(R.drawable.rect_divider_0);
    }

    @Override
    public void onClick(View v) {
        if (!mNoClick) {
            return;
        }
        switch (v.getId()) {
            case 2:
//                InputNumDialog inputNumDialog = new InputNumDialog(mContext);
//                inputNumDialog.addOnNumChangeListener(num1 -> {
//                    if (TextUtils.isEmpty(num1)) {
//                        return;
//                    }
//                    setNum(Integer.valueOf(num1));
//                    if (mOnInputNumChangeListener != null) mOnInputNumChangeListener.change(num);
//                });
//                inputNumDialog.show();
                return;
            case 0:
                if (!isCar) {
                    if (num <= min)
                        return;
                    if (num <= 0) {
                        Toast.makeText(mContext, content, Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
                num -= step;
                if (num <= 0) {
                    num = 0;
                }
                break;
            case 1:
                if (num >= max) {
                    num = max;
                    Toast.makeText(mContext, content1 + max, Toast.LENGTH_SHORT).show();
                    return;
                }
                num += step;
                if (num >= max) {
                    num = max;
                }
                break;
        }

        if (listener != null) {
            listener.numberChange(num);
        }
        if(isCar && num <= 0 ){
            if (mOnInputNumChangeListener != null) {
                mOnInputNumChangeListener.change(0);
            }
            return;
        }else{
            if (mOnInputNumChangeListener != null) {
                mOnInputNumChangeListener.change(v.getId() == 0 ? -step : step);
            }
        }

        numTv.setText(num + "");
    }

    /**
     * 设置符号颜色及大小
     *
     * @param tv
     */
    private void setSymbolTextView(TextView tv, int id, CharSequence sequence, int resId) {
        this.setTextView(tv, id, sequence, symbolColor, symbolSize, resId);
    }


    public void setText(String content,String content1) {
        this.content = content;
        this.content1 = content1;
    }


    public void setIvGone(){
        minIv.setVisibility(View.GONE);
    }
    public void setStep(int step) {
        this.step = step;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public void setMin(int min){
        this.min = min;

    }
    public void setCar() {
        isCar = true;
    }


    private void setNumTextView(TextView tv) {
        this.setTextView(tv, 2, num + "", numTextColor, numTextSize, R.drawable.rect_divider_0);
    }

    private void setTextView(TextView tv, int id, CharSequence sequence, int color, int size, int resId) {
        tv.setId(id);
        tv.setTextColor(color);
        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
        tv.setText(sequence);
        tv.setGravity(Gravity.CENTER);
        tv.setBackgroundResource(resId);
        tv.setOnClickListener(this);
    }

    private TextView getTextView() {
        return new TextView(mContext);
    }

    public int getNumTextSize() {
        return numTextSize;
    }

    public void setNumTextSize(int numTextSize) {
        this.numTextSize = numTextSize;
    }

    public int getNumTextColor() {
        return numTextColor;
    }

    public void setNumTextColor(int numTextColor) {
        this.numTextColor = numTextColor;
    }

    public int getSymbolSize() {
        return symbolSize;
    }

    public void setSymbolSize(int symbolSize) {
        this.symbolSize = symbolSize;
    }

    public int getSymbolColor() {
        return symbolColor;
    }

    public void setSymbolColor(int symbolColor) {
        this.symbolColor = symbolColor;
    }

    /**
     * 设置当前数字
     *
     * @param num
     */
    public void setNum(int num) {
        this.num = num;
        numTv.setText(num + "");
    }

    public int getNum() {
        return num;
    }

    public NumControlView setListener(OnNumberChangeListener listener) {
        this.listener = listener;
        return this;
    }

    public NumControlView setMaxVlaue(int maxVlaue) {
        this.maxVlaue = maxVlaue;
        return this;
    }

    public void setNoClick(boolean noClick) {
        mNoClick = noClick;
    }

    public NumControlView addOnInputNumChangeListener(OnInputNumChangeListener onInputNumChangeListener) {
        mOnInputNumChangeListener = onInputNumChangeListener;
        return this;
    }

    public interface OnInputNumChangeListener {
        void change(int num);
    }
}
